package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.ProjectService;
import hu.bme.aut.consulttime.client.ProjectServiceAsync;
import hu.bme.aut.consulttime.client.StudentTeamMembershipService;
import hu.bme.aut.consulttime.client.StudentTeamMembershipServiceAsync;
import hu.bme.aut.consulttime.client.TeamService;
import hu.bme.aut.consulttime.client.TeamServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.StudentTeamMembershipDTO;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TeamPage extends Composite {

	private static ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);
	
	private static StudentTeamMembershipServiceAsync stmService = GWT
			.create(StudentTeamMembershipService.class);
	
	private static TeamServiceAsync teamService = GWT.create(TeamService.class);

	private static Button btnCreateGroup;
	private static VerticalPanel mainPanel;
	private static FlexTable flexTable;

	private static List<ProjectDTO> projectList;
	private static List<String> semesterList;

	public TeamPage() {
		mainPanel = new VerticalPanel();
		initWidget(mainPanel);

		btnCreateGroup = new Button("Create New Team");
		btnCreateGroup.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				teamService
						.retrieveSemesters(new AsyncCallback<List<String>>() {
							@Override
							public void onSuccess(List<String> result) {
								semesterList = result;
								projectService
										.retrieveProjects(new AsyncCallback<List<ProjectDTO>>() {
											@Override
											public void onSuccess(
													List<ProjectDTO> result) {
												projectList = result;
												new CreateTeam(projectList,
														semesterList).center();
											}

											@Override
											public void onFailure(
													Throwable caught) {
												Window.alert("Remote Procedure Call - Failure");
											}
										});
							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Remote Procedure Call - Failure");
							}
						});
			}
		});
		mainPanel.add(btnCreateGroup);
		mainPanel.setCellHorizontalAlignment(btnCreateGroup,
				HasHorizontalAlignment.ALIGN_RIGHT);

		//loadTeams();
	}

	public static void loadTeams() {
		if (flexTable != null) {
			mainPanel.remove(flexTable);
		}
		flexTable = new FlexTable();
		flexTable.setBorderWidth(1);
//		flexTable.setWidth("100%");

		// Add these lines
		flexTable.setText(0, 0, "Team Name");
		flexTable.setText(0, 1, "Project Id");
		flexTable.setText(0, 2, "Semester");
		flexTable.setText(0, 3, "Team Members");
		flexTable.setText(0, 4, "Datas");
		flexTable.setText(0, 5, "Members");
		flexTable.setText(0, 6, "ConsultTimes");
		flexTable.setText(0, 7, "Remove");

		teamService.retrieveTeams(new AsyncCallback<List<TeamDTO>>() {

			@Override
			public void onSuccess(List<TeamDTO> allTeams) {
				for (int i = 0; i < allTeams.size(); ++i) {
					final TeamDTO tmpTeam = allTeams.get(i);

					flexTable.setText(i + 1, 0, tmpTeam.getName());
					flexTable.setText(i + 1, 1,
							String.valueOf(tmpTeam.getProjectId()));
					flexTable.setText(i + 1, 2, tmpTeam.getSemesterId());
					
					String memberNameList = "";
					java.util.Iterator<UserDTO> it = tmpTeam.getMembers().iterator();
					while(it.hasNext()) {
						memberNameList += it.next().getUserName();
				        if(it.hasNext()){
				        	memberNameList += ", ";
				        }
				      }
					
					flexTable.setText(i + 1, 3, memberNameList);

					// add button to modify this user from the list
					Button modifyUser = new Button("Modify");
					
					modifyUser.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							teamService
									.retrieveSemesters(new AsyncCallback<List<String>>() {
										@Override
										public void onSuccess(
												List<String> result) {
											semesterList = result;
											projectService
													.retrieveProjects(new AsyncCallback<List<ProjectDTO>>() {
														@Override
														public void onSuccess(
																List<ProjectDTO> result) {
															projectList = result;
															new ModifyTeam(tmpTeam, projectList, semesterList).center();
														}

														@Override
														public void onFailure(
																Throwable caught) {
															Window.alert("Remote Procedure Call - Failure");
														}
													});
										}

										@Override
										public void onFailure(Throwable caught) {
											Window.alert("Remote Procedure Call - Failure");
										}
									});
						}
					});

					flexTable.setWidget(i + 1, 4, modifyUser);

					
					// add button to modify this user from the list
					Button manageMembers = new Button("Modify");
					manageMembers.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							new ModifyTeamMembers(tmpTeam).center();
						}
					});

					flexTable.setWidget(i + 1, 5, manageMembers);
					
					// add button to remove this user from the list
					Button modifyConsultTime = new Button("Modify");
					modifyConsultTime.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							stmService.retrieveStudentTeamMemberships(tmpTeam, new AsyncCallback<List<StudentTeamMembershipDTO>>() {
								
								@Override
								public void onSuccess(List<StudentTeamMembershipDTO> smtList) {
									new ModifyConsultTime(tmpTeam, smtList).center();
								}
								
								@Override
								public void onFailure(Throwable caught) {
									Window.alert("Remote Procedure Call - Failure");
								}
							});
							
						}
					});

					flexTable.setWidget(i + 1, 6, modifyConsultTime);
					
					// add button to remove this user from the list
					Button removeUser = new Button("Delete");
					removeUser.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							// Remove the User - inner AsyncCall
							teamService.deleteTeam(tmpTeam.getTeamId(),
									new AsyncCallback<Boolean>() {

										@Override
										public void onSuccess(Boolean result) {
											if (result == true) {
												Window.confirm("'"
														+ tmpTeam.getName()
														+ "' team deleted!");
										        //Refresh the table
										        loadTeams();
											} else {
												Window.alert("Team deletion failed!");
											}
										}

										@Override
										public void onFailure(Throwable caught) {
											Window.alert("Remote Procedure Call - Failure");
										}
									});
						}
					});

					flexTable.setWidget(i + 1, 7, removeUser);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}
		});
		mainPanel.add(flexTable);
	}
}
